#pragma once
#include <QString>
#include <LayoutView.h>
#include <fpp.h>
#include "objectvisual.h"
#include "PrefabRepository.h"

#include "Layout.h"
#include "ObjectPrefab.h"
class LayoutController : public QObject
{
	Q_OBJECT
public:
	LayoutController(void);
	~LayoutController(void);

public:
	void SwitchToLayout(const QString& path)
	{
		SwitchToLayout(Layout::Load(path));
	}
	void SwitchToLayout(Layout* layout)
	{
		layoutView_->Reset();
		if(activeLayout_ != nullptr)
			delete activeLayout_;
		activeLayout_ = layout;
		auto allObjs = activeLayout_->GetAllObjects();
		for(auto it = allObjs->begin(); it != allObjs->end(); it++)
		{
			LayoutObjectAdded(*it);
		}

		
		connect(activeLayout_, SIGNAL(ObjectAdded(LayoutObject*)), 
			SLOT(LayoutObjectAdded(LayoutObject*)));
		connect(activeLayout_, SIGNAL(ObjectRemoved(LayoutObject*)), 
			SLOT(LayoutObjectRemoved(LayoutObject*)));
	}
	void SaveLayout();
	void addFurniture();
public slots:
	void deleteClicked();
	void prefabAdded(ObjectPrefab* prefab);
	void newClicked();
	void exitClicked();
	void placeItem(QListWidgetItem*);
	void saveClicked();
	void loadClicked();
	void saveToFile();
	void loadFromFile();
	
	void zoomInClicked()
	{		
		layoutView_->scale(2, 2);
	}
	void zoomOutClicked()
	{
		
		layoutView_->scale(.5, .5);
	}
	void ViewSelectionChanged() 
	{
		
		auto currentSelections = layoutView_->getSelectedItem();
		if(currentSelections.size() > 0)
		{
			ObjectVisual * visual = (ObjectVisual*)currentSelections.at(0);
			ui_.ui.infoView->setPixmap(*(visual->LayoutObj()->Prefab()->Pixmap()));
		}
		else
		{
			ui_.ui.infoView->clear();
		}
		
	}


	//These 2 slots sync  the model and the view
	void LayoutObjectAdded(LayoutObject* newModelObject)
	{
		//set up a new view object that's a repr. of the model object
		layoutView_->SyncNewObject(newModelObject);	
	}

	void LayoutObjectRemoved(LayoutObject* oldModelObject)
	{
		layoutView_->DesyncObject(oldModelObject);
	}
	void Run()
	{
		ui_.show();
	}

private:
	Layout* activeLayout_;
	LayoutView* layoutView_;
	fpp ui_;
	int scaleCur_;
};

